<template>
  <div>
    <tableComponents :isHavePagination="false" :isShow="false" :isHaveExport="false" @toAdd="addmodule">
      <template v-slot:table>
        <el-row class="tl">
          <index-list-item @sendDel="sendDel" v-for="(item, i) in list" :item="item" :key="i" @confirmitem="confirmitem" :urlHeader="urlHeader"
              @refresh="getIndexLists" @copymodule="copymodule" />
        </el-row>
      </template>
    </tableComponents>
    <el-dialog :close-on-click-modal="false" :title="iscopy == true ? '复制模板' : '添加模板'" class="tl size_13" width="800px"
        :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="form">
        <el-form-item label="iden">
          <el-col :span="14">
            <el-input v-model="form.iden"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="标题">
          <el-col :span="14">
            <el-input v-model="form.title"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item class="tc">
          <el-button :loading="$store.state.isLoading" type="primary" @click="addIndexListModule">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <preview :show="dia_show" :info="sel_item" @closedia="dia_show = false"></preview>
  </div>
</template>

<script>
import indexListItem from "./index-list-item/index-list-item";

import preview from "@/components/page/index/index-list/index-list-item/preview.vue";
export default {
  data () {
    return {
      iscopy: false,
      urlHeader: "",
      sel_item: {},
      dia_show: false,
      list: [],
      dialogFormVisible: false,
      form: {
        iden: "",
        title: ""
      }
    };
  },
  components: {
    indexListItem,
    preview
  },
  created () {
    this.geturlheader();
    this.getIndexLists();
  },
  mounted () { },
  methods: {
    addmodule () {
      this.iscopy = false;
      this.form = {
        iden: "",
        title: ""
      };
      this.dialogFormVisible = true;
    },
    copymodule (item) {
      this.iscopy = true;
      this.form = {
        iden: "",
        title: "",
        id: item.id
      };
      this.dialogFormVisible = true;
    },
    confirmitem (item) {
      this.sel_item = item;
      this.dia_show = true;
    },
    geturlheader () {
      this.$api.getSettings.getSettingsList({ cate: "wx" }).then(res => {
        if (res.data.code == 200 && res.data.status == 1) {
          res.data.result.forEach(item => {
            if (item.title == "手机端网址") {
              this.urlHeader = item.value;
            }
          });
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    sendDel (id) {
      this.$api.getPage.delPageIndexListItem({ id }).then(res => {
        if (res.data.code == 200) {
          this.$fnc.alertSuccess("删除成功");
          this.getIndexLists();
        } else {
          this.$fnc.alertError(res.data.result);
        }
      });
    },
    getIndexLists () {
      this.$api.getPage.getIndexLists({}).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        } else {
          this.$fnc.alertError(res.data.result);
        }
      });
    },
    addIndexListModule () {
      var params = {};
      params = this.form;
      if (this.iscopy == true) {
        this.$api.getPage.copyIndexModules(params).then(res => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$message({
              message: "复制成功",
              type: "success"
            });
            setTimeout(() => {
              this.getIndexLists();
            }, 1000);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      } else {
        this.$api.getPage.addIndexListModule(params).then(res => {
          if (res.data.code == 200) {
            this.dialogFormVisible = false;
            this.$message({
              message: "添加成功",
              type: "success"
            });
            setTimeout(() => {
              this.getIndexLists();
            }, 1000);
          } else {
            this.$fnc.alertError(res.data.result);
          }
        });
      }
    }
  }
};
</script>

<style scoped lang="less"></style>
